import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutterapp/models/room_list_item_data.dart';
import 'package:flutterapp/page/home/tab_search/dataList.dart';
import 'package:flutterapp/page/utils/dio_http.dart';
import 'package:flutterapp/page/utils/scopoed_model_helper.dart';
import 'package:flutterapp/page/widgets/common_floating_action_button.dart';
import 'package:flutterapp/page/widgets/room_list_item_widget.dart';
import 'package:flutterapp/scoped_model/auth.dart';

//房屋管理详情页面
class RoomManagePage extends StatefulWidget {
  const RoomManagePage({Key key}) : super(key: key);

  @override
  _RoomManagePageState createState() => _RoomManagePageState();
}

class _RoomManagePageState extends State<RoomManagePage> {
  //房屋管理............... 发送请求 已经租了的
  List<RoomListItemData> availabledDtatList = [];

  //房屋管理............... 发送请求 空置的
  List<RoomListItemData> availabledDtatListNot = [];

  _getData() async {
    //
    print('获取token................');
    //从状态获取token
    var auth = ScopedModelHelper.getModel<AuthModel>(context);
    print(auth);
    //判断token是否登录
    if (!auth.isLogin) return;
    // not get token--------------------------------------------------------------
    var token = auth.token.toString();

    //这里获取接口列表..........................................
    String url = '/houses/list';
    var res = await DioHttp.of(context).get(url);
    var resMap = json.decode(res.toString());
    //解构数据 返回的是搜索到的列表数据..............................r
    List dataListItem = resMap['body'];
    //返回的数据类型转换
    List<RoomListItemData> resList =
        dataListItem.map((json) => RoomListItemData.fromJson(json)).toList();
    setState(() {
      availabledDtatList = resList;
      availabledDtatListNot = resList;
    });

    // 获取数据并赋值......................
  }

  //添加生命周期
  @override
  void initState() {
    //延迟执行生命周期
    Timer.run(_getData);
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      initialIndex: 0,
      child: Scaffold(
          //位置 居中
          floatingActionButtonLocation:
              FloatingActionButtonLocation.centerFloat,
          //相当于绝对引用......底部组件 浮动效果   下面是重构的组件 CommonFloatingActionButton(),
          floatingActionButton: CommonFloatingActionButton('发布房源', () {
            //获取返回上一层的值 pop  里面的true
            var result = Navigator.of(context).pushNamed('roomAdd');
            result.then((value) => () {
                  if (value) {
                    _getData();
                  }
                });
          }),
          appBar: AppBar(
            title: Text('房屋管理'),
            centerTitle: true,
            bottom: TabBar(tabs: [
              Tab(
                text: '已租',
              ),
              Tab(
                text: '空置',
              ),
            ]),
          ),
          body: TabBarView(children: [
            ListView(
              children:
                  availabledDtatList.map((e) => RoomListItemWidget(e)).toList(),
            ),
            ListView(
              children: availabledDtatListNot
                  .map((e) => RoomListItemWidget(e))
                  .toList(),
            ),
          ])),
    );
  }
}
